<?

	function servicepreferred($type)
	{
		if($type==0)
		{
			return "Pick From Store";
		}
		else
		{
			return getDataById("os_shipping_company","company_name","id",$type);
		}
		
	}
	
	function getTypeName($type)
	{
		switch($type)
		{
			case "aboutus":
				$title="About Us";
				break;
			case "dealers":
				$title="Dealers &amp; Techies";
				break;	
			case "info":
				$title="Info";
				break;		
			case "faq";	
				$title="FAQ";
				break;
			case "tnc";	
				$title="Terms and Conditions";
				break;
			case "privacy_policy";	
				$title="Privacy Policy";
				break;		
										
			default:
				$title="Invalid! Unknown content type";
				break;
		}
	
		return $title;
	}
	
	
	function generatePassword() {
	
		$salt = "1234567890"; 
	
		srand((double)microtime()*1000000);  
	
		$i = 2; 
		$pass="";
		while ($i < 10) {  // change for other length
			$num = rand() % 33; 
			$tmp = substr($salt, $num, 1); 
			$pass = $pass . $tmp; 
			$i++; 
		} 
		return $pass;
	}
	
	
	
	function checkDateBeforeAfter($dt_given, $dt_check, $type){
	  if($type=="after")
	  {
		if(strtotime($dt_check) >= strtotime($dt_given)){
			return true;
		}else{
			return false;
		}
	  }
	  else{
		if(strtotime($dt_check) <= strtotime($dt_given)){
			return true;
		}else{
			return false;
		}
	  }
	}
	
	function discountVoucherErrorMsg($error_code)
	{
		$return="";
		switch($error_code)
		{
			case(11):
				$return="Invalid voucher code";
				break;
			case(12):
				$return="Invalid voucher. Voucher had been previously used!";
				break;
			case(13):
				$return="Failed to retrieve member data!";
				break;	
			case(14):
				$return="Sorry,the voucher is expired";
				break;
			case(15):
				$return="The minimum purchase in order to use this voucher is RM".$_SESSION['minimum_purchase'];
				break;
			case(16):
				$return="Exceeded used limit!";
				break;	
			
			default:
				$return="Unknown Error!";
				break;						
		}
		return "<div class='dw_failed'>".$return."</div>";
	}
	
	function checkDiscountVoucher($value)
	{
		$flag=0;
		$chk=mysql_query("select * from os_discount_voucher where voucher_code='$value'")or die(mysql_error());
			
		if(mysql_num_rows($chk)==0)
		{
			$flag=11; // invalid voucher code
		}
		else
		{
			$member_id=$_SESSION['loginid'];
			$v=mysql_fetch_array($chk,MYSQL_ASSOC);
			$voucher_id=$v['id'];
			$times_to_use=$v['times_to_use'];
			$is_birthday_voucher=$v['is_birthday_voucher'];
			$minimum_purchase=$v['minimum_purchase'];
			$_SESSION['minimum_purchase']=$minimum_purchase;		
			if($is_birthday_voucher==1)
			{		
				$get_dob=mysql_query("select dob from os_member where id=$member_id");
				if(mysql_num_rows($get_dob)==0)
				{
					$flag=13; //Failed to retrieve member data!
				}
				else
				{
					$chk_used=mysql_query("select count(*) as result from os_discount_voucher_used where member_id='$member_id' and voucher_id='$voucher_id'");
					$voucher_used=mysql_result($chk_used,0,"result");
					
					if($voucher_used>=$times_to_use)
					{
						$flag=12; //Invalid voucher. Voucher had been previously used!					
					}
					else
					{
						$date_flag=true;
						$days_before="-".$v['send_from_day']." day";
						$days_after="+".$v['expiry_day']." day";
						$birthday_year=$v['birthday_year'];
						$now_date=date("Y-m-d");
						$dob=mysql_result($get_dob,0,"dob");
						$dob_date=date("d",strtotime($dob));
						$dob_month=date("m",strtotime($dob));
						
						$valid_dob=$birthday_year."-".$dob_month."-".$dob_date;			
						$start_date=date("Y-m-d",strtotime($days_before,strtotime($valid_dob)));
						$end_date=date("Y-m-d",strtotime($days_after,strtotime($valid_dob)));
						
						if(!checkDateBeforeAfter($start_date, $now_date, "after"))
						{
							$date_flag=false;
						}
						
						if($v['expiry_day']>0)
						{
							if(!checkDateBeforeAfter($end_date, $now_date, "before"))
							{
								$date_flag=false;
							}
						}
						
						if($date_flag==false)
						{
							$flag=14; //Sorry,the voucher is expired
						}
						else
						{
							
							
							if($minimum_purchase>$_SESSION['total_overall_price'])
							{
								$flag=15; //The minimum purchase in order to use this voucher is RM $minimum_purchase . 
								
							}
							else
							{
								$flag=1; //success
							}
							
						}
					}						
				}								
			}
			else // Normal discount voucher
			{
				$chk_used=mysql_query("select count(*) as result from os_discount_voucher_used where member_id='$member_id' and voucher_id='$voucher_id' and status=0");
				
				if(mysql_result($chk_used,0,"result")>0)
				{
					$date_flag=true;
					$now_date=date("Y-m-d");
					$start_date=$v['start_date'];
					$end_date=$v['end_date'];
					
					if(!checkDateBeforeAfter($start_date, $now_date, "after"))
					{
						$date_flag=false;
					}
					
					if($v['expiry_day']>0)
					{
						if(!checkDateBeforeAfter($end_date, $now_date, "before"))
						{
							$date_flag=false;
						}
					}
					
					if($date_flag==false)
					{
						$flag=14; //Sorry,the voucher is expired
					}
					else
					{
						$minimum_purchase=$v['minimum_purchase'];
						if($minimum_purchase>$_SESSION['total_overall_price'])
						{
							$flag=15; //The minimum purchase in order to use this voucher is RM $minimum_purchase . 
							
						}
						else
						{
							$flag=1; //success
						}
						
					}
				}
				else
				{
					$flag=11; //exceeded limit!
				}			
			}			
			
		}
	
		return $flag;
	}
		
	function convertDate($date,$style){
		
		$splitAll=explode(" ",$date);
		
		
		if(count($splitAll)==2)// with time
		{
			$splitDate=explode("-",$splitAll[0]);
			$day=$splitDate[2];
			$month=$splitDate[1];
			$year=$splitDate[0];
			
			$splitTime=explode(":",$splitAll[1]);
			$hour=$splitTime[0];
			$min=$splitTime[1];
			$sec=$splitTime[2];
			
			$detailedDate=date($style,mktime($hour,$min,$sec,$month,$day,$year));
		}elseif(count($splitAll)==1){ //without time
			
			$splitDate=explode("-",$date);
			$day=$splitDate[2];
			$month=$splitDate[1];
			$year=$splitDate[0];
			$hour=0;
			$min=0;
			$sec=0;
			
			$detailedDate=date($style,mktime($hour,$min,$sec,$month,$day,$year));
		}else{
		
			$detaildDate="Invalid format";
		}
		
		return $detailedDate;
	}
	
	
	
	
	
	function dayDiff($time)
	{	
		$split_str=explode(" ",$time);
		$date=explode("-",$split_str[0]);
		$time=explode(":",$split_str[1]);
		
		$hour=$time[0];
		$min=$time[1];
		$sec=$time[2];
		
		$year=$date[0];
		$month=$date[1];
		$day=$date[2];
		
		$seconds_dif = mktime(date("H")+8,date("i"),date("s"),date("m"),date("d"),date("Y")) - mktime($hour,$min,$sec,$month,$day,$year); 
		$day=24*60*60;
		
		$day_diff = floor($seconds_dif/$day);
		return $day_diff;
	}
	
	
	
	function birthday($birthday)
	{
		list($year,$month,$day) = explode("-",$birthday);
		$year_diff  = date("Y") - $year;
		$month_diff = date("m") - $month;
		$day_diff   = date("d") - $day;
		if ($month_diff < 0) $year_diff--;
		elseif (($month_diff==0) && ($day_diff < 0)) $year_diff--;
		return $year_diff;
	}
	
	function minDiff($time)
	{	
		$split_str=explode(" ",$time);
		$date=explode("-",$split_str[0]);
		$time=explode(":",$split_str[1]);
		
		$hour=$time[0];
		$min=$time[1];
		$sec=$time[2];
		
		$year=$date[0];
		$month=$date[1];
		$day=$date[2];
		
		$seconds_dif = mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y")) - mktime($hour,$min,$sec,$month,$day,$year); 
		
		$min_diff = floor($seconds_dif/60);
		return $min_diff;
	}
	
	
	
	
	function timeDiff($time, $opt = array())
	{
		// The default values
		$defOptions = array(
			'to' => 0,
			'parts' => 1,
			'precision' => 'second',
			'distance' => TRUE,
			'separator' => ', '
		);
		$opt = array_merge($defOptions, $opt);
		// Default to current time if no to point is given
		(!$opt['to']) && ($opt['to'] = time());
		// Init an empty string
		$str = '';
		// To or From computation
		$diff = ($opt['to'] > $time) ? $opt['to']-$time : $time-$opt['to'];
		// An array of label => periods of seconds;
		$periods = array(
			'decade' => 315569260,
			'year' => 31556926,
			'month' => 2629744,
			'week' => 604800,
			'day' => 86400,
			'hour' => 3600,
			'minute' => 60,
			'second' => 1
		);
		// Round to precision
		if ($opt['precision'] != 'second')
			$diff = round(($diff/$periods[$opt['precision']])) * $periods[$opt['precision']];
		// Report the value is 'less than 1 ' precision period away
		(0 == $diff) && ($str = 'less than 1 '.$opt['precision']);
		// Loop over each period
		foreach ($periods as $label => $value) {
			// Stitch together the time difference string
			(($x=floor($diff/$value))&&$opt['parts']--) && $str.=($str?$opt['separator']:'').($x.' '.$label.($x>1?'s':''));
			// Stop processing if no more parts are going to be reported.
			if ($opt['parts'] == 0 || $label == $opt['precision']) break;
			// Get ready for the next pass
			$diff -= $x*$value;
		}
		$opt['distance'] && $str.=($str&&$opt['to']>$time)?' ago':' away';
		return $str;
	}

?>
